package com.chinacache.pqbs.entity.user;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

/**
 * User entity.
 * 
 * 用MyEclipse自动生成的JPA annotation注明ORM关系. 用@Cache注明Hibernate缓存策略.
 * 
 * @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "USERS")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class User extends BaseEntity
{

	// Fields

	private Integer id;
	private String loginName;
	private String password;
	private String name;
	private String email;
	private String department;
	private String status;
	private String tel;

	private Set<Role> roles = new HashSet<Role>(0);

	// Constructors

	/** default constructor */
	public User()
	{
		//
	}

	/** full constructor */
	public User(Integer id, String loginName, String password, String name, String email, Set<Role> roles)
	{
		this.id = id;
		this.loginName = loginName;
		this.password = password;
		this.name = name;
		this.email = email;
		this.roles = roles;
	}

	// Property accessors
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "ID", unique = true, nullable = false, insertable = true, updatable = true)
	public Integer getId()
	{
		return id;
	}

	public void setId(Integer id)
	{
		this.id = id;
	}

	@Column(name = "LOGIN_NAME", unique = true, nullable = false, insertable = true, updatable = true, length = 20)
	public String getLoginName()
	{
		return loginName;
	}

	public void setLoginName(String loginName)
	{
		this.loginName = loginName;
	}

	@Column(name = "PASSWORD", unique = false, nullable = true, insertable = true, updatable = true, length = 64)
	public String getPassword()
	{
		return password;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

	@Column(name = "NAME", unique = false, nullable = true, insertable = true, updatable = true, length = 20)
	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	@Column(name = "EMAIL", unique = false, nullable = true, insertable = true, updatable = true, length = 50)
	public String getEmail()
	{
		return email;
	}

	public void setEmail(String email)
	{
		this.email = email;
	}

	@ManyToMany(fetch = FetchType.LAZY, cascade =
	{ CascadeType.PERSIST, CascadeType.REFRESH })
	@JoinTable(name = "USERS_ROLES")
	public Set<Role> getRoles()
	{
		return roles;
	}

	public void setRoles(Set<Role> roles)
	{
		this.roles = roles;
	}

	@Override
	public String toString()
	{
		return ToStringBuilder.reflectionToString(this);
	}

	@Transient
	public boolean isEnabled()
	{
		// return (this.status != null && !this.status.equals("0"));
		return true;
	}

	public String getDepartment()
	{
		return department;
	}

	public void setDepartment(String department)
	{
		this.department = department;
	}

	public String getStatus()
	{
		return status;
	}

	public void setStatus(String status)
	{
		this.status = status;
	}

	public String getTel()
	{
		return tel;
	}

	public void setTel(String tel)
	{
		this.tel = tel;
	}
}